<template>
    <div class="login-page">
        <div class="block">
            <input type="text" placeholder="输入手机号码" v-model="username">
        </div>
        <div class="block">
            <input type="password" placeholder="输入密码" v-model="password">
        </div>
        <div class="block">
            <input type="button" value="登陆" @click="login">
        </div>
    </div>
</template>

<script>
import Axios from 'axios'
import {Toast } from 'mint-ui'
import { mapActions } from 'vuex'


export default {
    data(){
        return {
            username:'',
            password:''
        }
    },
    methods:{
        ...mapActions(['request_user_info']),
        login(){
            //设置基准路径
            Axios.defaults.baseURL = "/api/";
            Axios.post('/login',{ username:this.username,password: this.password }).then((res)=>{
            
                if(res.data.code == 200)
                //登陆成功,保存加密字符串token到localStorage, 跳转到首页
                {
                    localStorage.setItem('token',res.data.token);
                    //请求用户信息
                    this.request_user_info( res.data.id );

                    this.$router.push('/home');
                }
                else//登陆失败, 弹框提示
                {
                    Toast('用户名/密码错误!');
                }
            })
        }
    }
}
</script>
<style scoped>
.login-page .block{
    padding:  20px;
}
.login-page .block input{
    box-sizing: border-box;
    padding-left: 20px;
    width: 100%;
    line-height: 50px;
    border: none;
    border-bottom: 1px solid #f5f5f5;
    outline: none;
}
.login-page .block input[type='button']{
    border-radius: 25px;
}
</style>